﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FortuneHeavenCommon.DataMap;
using FortuneHeavan.Game.DAL;
using FortuneHeavenDAL;

namespace FortuneHeavenBAL
{
    public class RechargeCodeBO
    {
        public RechargeCodeDAL _payDAL;
        public RechargeCodeBO()
            : base()
        {
            _payDAL = new RechargeCodeDAL();
        }

        /// <summary>
        /// 卡生效
        /// </summary>
        /// <param name="list">需要生效的卡的ID号</param>
        /// <returns></returns>
        public bool Valid(IList<int> list)
        {
            return _payDAL.Valid(list);
        }

        /// <summary>
        /// 用充值卡给用户充值
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="code">卡号</param>
        ///<param name="msg">返回信息</param> 
        /// <returns>成功返回true,失败返回false</returns>
        public bool AddScoreByCard(string userName,string code,out string msg)
        {
            string encode = FortuneHeavenCommon.ComMethod.CommonEncrypt.Encode(code);
            RechargeCodeDAL rechargeCodeDAL = new RechargeCodeDAL();
            RechargeCodeRecord  rechargeCode = rechargeCodeDAL.GetRechargeCode(encode);
            if (rechargeCode == null)
            {
                msg = "卡号或密码输入错误";
                return false;
            }
            if (rechargeCode.Status == 0)
            {
                msg = "此卡处于停用状态，请联系网站客服开启";
                return false;
            }
            if (rechargeCode.Status == 2)
            {
                msg = "此卡已经使用过，不可再充值";
                return false;
            }
            AccountsInfoDAL accountsInfoDAL = new AccountsInfoDAL();
            AccountsInfo accountsInfo = accountsInfoDAL.GetAcount(userName);
            if (accountsInfo == null)
            {
                msg =string.Format("用户“{0}”不存在",userName);
                return false;
            }
            NetBarDAL netBarDAL = new NetBarDAL();
            NetBarInfo netBar = netBarDAL.GetNetBarInforsNameIDGoldValue(rechargeCode.NetBarId);
            double goldvalue = 1;
            if (netBar != null && netBar.GoldValue != null && netBar.GoldValue > 0)
            {
                goldvalue = netBar.GoldValue;
            }
            int addScore = (int)(goldvalue * rechargeCode.RechargeValue);

            if (rechargeCodeDAL.Recharge(rechargeCode.ID, accountsInfo.UserID))
            {
                GameScoreInfoDAL gameScoreInfoDAL = new GameScoreInfoDAL();
                GameScoreInfo score = gameScoreInfoDAL.GetGameScore(accountsInfo.UserID);
                if (score == null)
                {
                    score = new GameScoreInfo();
                    score.Score = 0;
                    score.Revenue = 0;
                    score.InsureScore = 0;
                    score.WinCount = 0;
                    score.LostCount = 0;
                    score.DrawCount = 0;
                    score.FleeCount = 0;
                    score.UserRight = 0;
                    score.AllLogonTimes = 0;
                    score.PlayTimeCount = 0;
                    score.UserID = accountsInfo.UserID;
                    score.OnLineTimeCount = 0;
                    if (accountsInfo != null)
                    {
                        score.RegisterIP = accountsInfo.RegisterIP;
                        score.RegisterDate = accountsInfo.RegisterDate;
                        score.LastLogonDate = accountsInfo.LastLogonDate;
                        score.LastLogonIP = accountsInfo.LastLogonIP;
                    }
                    else
                    {
                        score.RegisterIP = "1";
                        score.RegisterDate = DateTime.Now;
                        score.LastLogonDate = DateTime.Now;
                        score.LastLogonIP = "1";
                    }
                    gameScoreInfoDAL.Add(score);
                }
                if (gameScoreInfoDAL.AddInsureScore(addScore, accountsInfo.UserID))
                {
                    msg = "充值成功,此卡不能再用";
                    return true;
                }
                else
                {
                    msg = "充值失败";
                    if (rechargeCodeDAL.Valid(rechargeCode.ID))
                    {
                        msg += ",请再尝试";
                    }
                    else
                    {
                        msg += ",请联系网站客服";
                    }
                    return false;
                    /*此处最好能加入日志功能*/
                }
            }
            else
            {
                msg = "充值失败";
                return false;
            }

        }

        public List<RechargeCodeRecord> GetList(int page, int size, int isUsed, int NetBarId)
        {
            return _payDAL.GetList(page, size, isUsed,NetBarId);
        }
        public int GetTotalSize(int isUsed,int NetBarId)
        {
            return _payDAL.GetTotalSize(isUsed,NetBarId);
        }

        public void Save(RechargeCodeRecord bo)
        {
            _payDAL.Save(bo);
        }

        public void Add(RechargeCodeRecord bo)
        {
            _payDAL.Add(bo);
        }
        public RechargeCodeRecord GetRechargeCode(string code)
        {
            return _payDAL.GetRechargeCode(code);
        }
    }
}
